#!/usr/bin/env python3
"""
简化版 Nuitka 打包脚本 - 避免复杂选项
"""

import os
import sys
import subprocess
import platform
from datetime import datetime

def debug_print(msg):
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(f"[{timestamp}] {msg}")

def build_simple():
    """简化版构建流程"""
    app_name = "tms-controller"
    system = platform.system().lower()
    
    # 基本命令
    cmd = [
        sys.executable, "-m", "nuitka",
        "--standalone",
        "--onefile",
        "--remove-output",
        f"--output-filename={app_name}",
        "main.py"
    ]
    
    # 平台特定选项
    if system == "windows":
        cmd.append("--windows-disable-console")
        if os.path.exists("favicon.ico"):
            cmd.append("--windows-icon-from-ico=favicon.ico")
    elif system == "darwin":
        cmd.append("--macos-disable-console")
    else:  # linux
        cmd.append("--disable-console")
        if os.path.exists("favicon.ico"):
            cmd.append("--linux-icon=favicon.png")
    
    # 数据文件包含
    data_files = [
        ("config.json", "config.json"),
        ("global_settings.py", "global_settings.py"),
        ("favicon.ico", "favicon.ico"),
        ("favicon.png", "favicon.png"),
        ("favicon.icns", "favicon.icns"),
    ]
    
    for src, dest in data_files:
        if os.path.exists(src):
            cmd.append(f"--include-data-files={src}={dest}")
    
    # 包含目录
    if os.path.exists("protocol_configs"):
        cmd.append("--include-data-dir=protocol_configs=protocol_configs")
    
    debug_print(f"执行命令: {' '.join(cmd)}")
    
    try:
        result = subprocess.run(cmd, check=True)
        debug_print("✅ 构建成功!")
    except subprocess.CalledProcessError as e:
        debug_print(f"❌ 构建失败: {e}")
        return False
    
    return True

if __name__ == "__main__":
    build_simple()